﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMS.Business;
using System.Threading;

namespace AMS.From
{
    public partial class MainForm : Form
    {
        #region member
        private readonly AMS.Business.AccessWeb access = new AMS.Business.AccessWeb();
        private readonly AccountInfoBLL account = new AccountInfoBLL();
        #endregion

        #region Init
        public MainForm()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
  
            //check account status and write in database
            //access.BatchAccessWeb();
            //get expried account from database
            //dgvExpried.DataSource = account.GetExpriedAccount();
        }
        #endregion

        #region Event
        /// <summary>
        /// Add Account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void accountToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddAccount addForm = new AddAccount();
            addForm.Show();
        }

        /// <summary>
        /// About
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("1.0.0.0");
            AccessWeb web = new AccessWeb();
        }

        /// <summary>
        /// Get all account
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AccountInfoBLL account = new AccountInfoBLL();
            dgvExpried.DataSource = account.GetAllAccountInfo();
        }

        /// <summary>
        /// Batch access
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //check account status and write in database
            //进度条显示更新状态
            new System.Threading.Thread(new System.Threading.ThreadStart(StartDownload)).Start();
        }


        /// <summary>
        /// 下载事件
        /// </summary>
        public void StartDownload()
        {
            Downloader downloader = new Downloader();
            downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
            downloader.Start();
            //要执行的业务。
            AccountInfoBLL account = new AccountInfoBLL();
            //check account status and write in database
            access.BatchAccessWeb();
            this.progressBar1.Value = 100;

            Wait();
            MessageBox.Show("Finished!");

            dgvExpried.DataSource = account.GetExpriedAccount();
            //Application.Exit();

        }

        //同步更新ui
        void downloader_onDownLoadProgress(long total, long current)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] { total, current });
            }
            else
            {
                this.progressBar1.Maximum = (int)total;
                this.progressBar1.Value = (int)current;
            }
        }
        //
        public class Downloader
        {
            //委托
            public delegate void dDownloadProgress(long total, long current);
            //事件
            public event dDownloadProgress onDownLoadProgress;
            //开始模拟工作
            public void Start()
            {
                for (int i = 0; i < 92; i++)
                {
                    if (onDownLoadProgress != null)
                        onDownLoadProgress(100, i);
                    System.Threading.Thread.Sleep(50);
                }
            }
        }

        private void Wait()//单位为毫秒   
        {
            int i = 0;
            while (i <= 1024 * 1024 * 1024)
            {
                i++;
            }
        }
        #endregion
    }
}
